package com.zbl.zblproject.contract.controller;



import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zbl.zblproject.contract.entity.Contract;
import com.zbl.zblproject.contract.entity.Rebates;
import com.zbl.zblproject.contract.service.ContractService;
import com.zbl.zblproject.core.entity.Result;
import com.zbl.zblproject.core.entity.Supplier;
import com.zbl.zblproject.core.mapper.SupplierMapper;
import com.zbl.zblproject.core.utils.ResultUtil;
import com.zbl.zblproject.core.utils.TimeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * @author youlanqiang
 * @version 1.0
 * @date 2019/3/14
 */
@RestController
@RequestMapping("/contract")
@Api(tags = {"合同接口"})
public class ContractController {

    @Autowired
    private ContractService contractService;

    @Autowired
    private SupplierMapper supplierMapper;


    @GetMapping("/page/{current}/{size}")
    @ApiOperation("分页")
    public Result pageInfo(@ApiParam("分页") @PathVariable Integer current,
                           @ApiParam("项数") @PathVariable Integer size,
                           @ApiParam("开始时间") @RequestParam("startTime") String startTime,
                           @ApiParam("结束时间") @RequestParam("endTime") String endTime,
                           @ApiParam("名称") @RequestParam("search") String search){
        IPage<Contract> page = new Page<>(current, size);
        page = contractService.selectPage(page, startTime, endTime, search);
        return ResultUtil.success("成功!", page);

    }


    @GetMapping("/delete/some")
    @ApiOperation("删除一堆合同")
    public Result delete(@ApiParam("合同ID集合") @RequestParam("id[]") Integer[] array){
        List<Integer> list = Arrays.asList(array);
        if(list.isEmpty()){
            return ResultUtil.error("合同ID异常!");
        }
        if(contractService.deleteContract(list)){
            return ResultUtil.success("删除成功!");
        }else{
            return ResultUtil.error("删除失败!");
        }
    }


//    @GetMapping("/delete/{id}")
//    @ApiOperation("删除合同")
//    public Result deleteContract(@ApiParam("合同ID") @PathVariable int id){
//        if(contractService.deleteContract(id)){
//            return ResultUtil.success("删除成功!");
//        }else{
//            return ResultUtil.error("删除失败！");
//        }
//    }

    @PostMapping("/update")
    @ApiOperation("更新合同")
    public Result updateContract(@ApiParam("合同编号") @RequestParam("json") String json){
        JSONObject object = JSONObject.parseObject(json);
        Contract contract = new Contract();
        contract.setContractId(object.getInteger("contractId"));
        contract.setNumber(object.getString("number"));
        contract.setSupplierId(object.getInteger("supplierId"));

        Supplier supplier = supplierMapper.selectById(contract.getSupplierId());
        if(supplier != null){
            contract.setSuppliersName(supplier.getSuppliersName());
            contract.setSuppliersAddress(supplier.getSuppliersAddr());
            contract.setSuppliersDesc(supplier.getSuppliersDesc());
            contract.setSuppliersTelephone(supplier.getHeadNumber());
        }else{
            contract.setSuppliersName("");
            contract.setSuppliersAddress("");
            contract.setSuppliersDesc("");
            contract.setSuppliersTelephone("");
        }

        contract.setAddTime(LocalDateTime.now());
        contract.setStartTime(TimeUtil.parseTime(object.getString("startTime")));
        contract.setEndTime(TimeUtil.parseTime(object.getString("endTime")));
        contract.setCashBack(object.getDouble("cashBack"));
        contract.setPurchasesHighest(object.getDouble("purchasesHighest"));
        contract.setRebatesHighest(object.getDouble("rebatesHighest"));
        contract.setPlanRebatesHighest(object.getDouble("planRebatesHighest"));
        contract.setPurchasesLowest(object.getDouble("purchasesLowest"));
        contract.setRebatesLowest(object.getDouble("rebatesLowest"));
        contract.setPlanRebatesLowest(object.getDouble("planRebatesLowest"));
        contract.setNote(object.getString("note"));
        contract.setComment(object.getString("comment"));

        try {
            if(contractService.updateContract(contract, object.getJSONArray("rebates").toJavaList(Rebates.class))){
                return ResultUtil.success("添加成功.");
            }else{
                return ResultUtil.error("添加失败!");
            }
        }catch(RuntimeException e){
            return ResultUtil.error(e.getMessage());
        }
    }


    @PostMapping("/add")
    @ApiOperation("添加合同")
    public Result addContract(@ApiParam("合同编号") @RequestParam("json") String json){
        JSONObject object = JSONObject.parseObject(json);
        Contract contract = new Contract();
        contract.setNumber(object.getString("number"));
        contract.setSupplierId(object.getInteger("supplierId"));


        Supplier supplier = supplierMapper.selectById(contract.getSupplierId());
        if(supplier != null){
            contract.setSuppliersName(supplier.getSuppliersName());
            contract.setSuppliersAddress(supplier.getSuppliersAddr());
            contract.setSuppliersDesc(supplier.getSuppliersDesc());
            contract.setSuppliersTelephone(supplier.getHeadNumber());
        }else{
            contract.setSuppliersName("");
            contract.setSuppliersAddress("");
            contract.setSuppliersDesc("");
            contract.setSuppliersTelephone("");
        }
        try {
            contract.setAddTime(LocalDateTime.now());
            contract.setStartTime(TimeUtil.parseTime(object.getString("startTime")));
            contract.setEndTime(TimeUtil.parseTime(object.getString("endTime")));
            contract.setCashBack(object.getDouble("cashBack"));
            contract.setPurchasesHighest(object.getDouble("purchasesHighest"));
            contract.setRebatesHighest(object.getDouble("rebatesHighest"));
            contract.setPlanRebatesHighest(object.getDouble("planRebatesHighest"));
            contract.setPurchasesLowest(object.getDouble("purchasesLowest"));
            contract.setRebatesLowest(object.getDouble("rebatesLowest"));
            contract.setPlanRebatesLowest(object.getDouble("planRebatesLowest"));
            contract.setNote(object.getString("note"));
            contract.setComment(object.getString("comment"));
        }catch(NullPointerException e){
            return ResultUtil.error("未填选完整!");
        }
        try {
            if(contractService.addContract(contract, object.getJSONArray("rebates").toJavaList(Rebates.class))){
                return ResultUtil.success("添加成功.");
            }else{
                return ResultUtil.error("添加失败!");
            }
        }catch(RuntimeException e){
            return ResultUtil.error(e.getMessage());
        }
    }



}
